<?php
require('fpdf.php');
require('fpdf_inc.php');
class PDF extends FPDF_MULTICELLTAG {
  function footer() {
      $this->SetFont('','',8);
      //Go to 1.5 cm from bottom
      $this->SetXY(10,-15);
      $this->MultiCell(0,4,'iZeit by Harry Jennerway'."\n".'http://www.izeit.nu/',0,L,0);
      
      $this->SetXY(10,-15);
      $orig = array("_P_","_T_");
      $replace = array($this->PageNo(),'pgs');
      $pagemsg = str_replace($orig,$replace,STR_PAGES);
      $this->AliasNbPages('pgs');
      $this->Cell(0,0,$pagemsg,0,R,0);
  }
  function SetWidths($w) {
  	$this->widths=$w;
  }
  function SetAligns($a) {
  	$this->aligns=$a;
  }
  function Row($data) {
  	$nb=0;
  	for($i=0;$i<count($data);$i++) {
  		$nb=max($nb,$this->NbLines($this->widths[$i],$data[$i]['data']));
    }
  	$h=5*$nb;
    if((array_key_exists('padding',$data[0])) || (array_key_exists('padding',$data[1]))) {
      $h = $h+(max($data[0]['padding'],$data[1]['padding'])*2);
    }
  	//Issue a page break first if needed
  	$this->CheckPageBreak($h);
  	for($i=0;$i<count($data);$i++) {
//var_dump($data);
  		$w=$this->widths[$i];
  		$a=isset($this->aligns[$i]) ? $this->aligns[$i] : 'L';
  		$x=$this->GetX();
  		$y=$this->GetY();
      if($data[$i]['filled'] == 'lblue') {
        $this->SetFillcolor(204,223,235);
        $this->Rect($x,$y,$w,$h,DF);
      } elseif($data[$i]['filled'] == 'orange') {
        $this->SetFillcolor(255,200,93);
        $this->Rect($x,$y,$w,$h,DF);
      } elseif($data[$i]['filled'] == 'grey') {
        $this->SetFillcolor(219,219,219);
        $this->Rect($x,$y,$w,$h,DF);
      } elseif($data[$i]['filled'] == 'white') {
        $this->SetFillcolor(255);
        $this->Rect($x,$y,$w,$h,DF);
      } else {
  //          $this->Rect($x,$y,$w,$h);
        $this->SetFillcolor(255);
        $this->Rect($x,$y,$w,$h,DF);
      }
      if(array_key_exists('align',$data[$i])) {
        $a = $data[$i]['align'];
      }
  // $data[$i]['data'] = 'Align:'.$a."\n".'Size:'.$data[$i]['size']."\n";
  //Print the text
      if(array_key_exists('padding',$data[$i])) {
        $this->SetXY($x+$data[$i]['padding'],$y+$data[$i]['padding']);
        $x = $x+($data[$i]['padding']*2);
        $w = $w-($data[$i]['padding']*2);
      }
  		$this->MultiCellTag($w,5,$data[$i]['data'],0,$a);
  
  //		$this->MultiCellTag($w,5,$data[$i]['data'],0,$a);
  		//Put the position to the right of the cell
  		$this->SetXY($x+$w,$y);
  	}
  	$this->Ln($h);	//Go to the next line
  }
  
  function Row_assoc($data) {
  //var_dump($data);
  	//Calculate the height of the row
  	$nb=0;
    $i = 0;
  	foreach($data as $key => $d) {
        if($key != 'meta') {
          $nb=max($nb,$this->NbLines($this->widths[$i],$d['data']));
          $i++;
        }
    }
  	$h=5*$nb;
    if(array_key_exists('meta',$data)) {
      if(array_key_exists('padding',$data['meta'])) {
        $padding = $data['meta']['padding'];
        $h = $h+($data['meta']['padding']*2);
      }
      if(array_key_exists('filled',$data['meta'])) {
        if($data['meta']['filled'] == 'lblue') {
          $this->SetFillcolor(204,223,235);
          $this->Rect($x,$y,$w,$h,DF);
        } elseif($data['meta']['filled'] == 'orange') {
          $this->SetFillcolor(255,200,93);
          $this->Rect($x,$y,$w,$h,DF);
        } elseif($data['meta']['filled'] == 'grey') {
          $this->SetFillcolor(219,219,219);
          $this->Rect($x,$y,$w,$h,DF);
        } elseif($data['meta']['filled'] == '207') {
          $this->SetFillcolor(207);
          $this->Rect($x,$y,$w,$h,DF);
        } elseif($data['meta']['filled'] == 'white') {
          $this->SetFillcolor(255);
          $this->Rect($x,$y,$w,$h,DF);
        }
      }
    }
    if(!isset($data['meta']['filled'])) {
      $this->SetFillcolor(255);
    }
  	$this->CheckPageBreak($h);
    foreach($data as $key => $e) {
      if($key != 'meta') {
    		$w=$this->widths[$key];
    		$a=isset($this->aligns[$key]) ? $this->aligns[$key] : 'L';
    		$x=$this->GetX();
    		$y=$this->GetY();
        $this->Rect($x,$y,$w,$h,DF);
        if(array_key_exists('align',$e)) {
          $a = $e['align'];
        }
        if(isset($padding)) {
          $this->SetXY($x+$padding,$y+$padding);
          $x = $x+($padding*2);
          $w = $w-($padding*2);
        }
    		$this->MultiCellTag($w,5,$e['data'],0,$a);
  	  	$this->SetXY($x+$w,$y);
        $i++;
      }
  	}
    $this->Ln($h);
  }
}
?>